package com.lemon.smartsafetynew.util.user;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.utils.app.SharedPreferencesHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.user.LoginNameVo;
import com.lemon.smartsafetynew.util.common.AppSharePreferenceUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrew on 2017/7/17.
 */

public class AccountUtil {

    /**
     * 判断是否登陆
     *
     * @return
     */
    public static boolean isLogin() {

        AppSharePreferenceUtil sharePreferenceUtil = AppSharePreferenceUtil.getInstance();
        return !StringUtils.isEmpty(sharePreferenceUtil.getString(SafeCloudConstants.CURRENT_USER, ""));

    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static User getUser() {

        AppSharePreferenceUtil sharePreferenceUtil = AppSharePreferenceUtil.getInstance();
        String userStr = sharePreferenceUtil.getString(SafeCloudConstants.CURRENT_USER, "");
        if (StringUtils.isEmpty(userStr)) {
            return null;
        } else {
            return new Gson().fromJson(userStr, User.class);
        }
    }

    /**
     * 保存用户信息
     *
     * @param userJson
     */
    public static void saveUser(String userJson) {

        AppSharePreferenceUtil.getInstance().putString(SafeCloudConstants.CURRENT_USER, userJson);

    }


    /**
     * 用户退出
     */
    public static void userLogout() {

        AppSharePreferenceUtil sharePreferenceUtil = AppSharePreferenceUtil.getInstance();
        sharePreferenceUtil.putString(SafeCloudConstants.CURRENT_USER, "");
        sharePreferenceUtil.putString(SafeCloudConstants.USER_TELEPHONE_NUMBER, "");
        sharePreferenceUtil.remove(SafeCloudConstants.JPUSH_BIND_STATUS);

        //TODO
    }

    /**
     * 获取用户列表
     * @return
     */
    public static List<LoginNameVo> getLoginListName() {
        try {
            String json = AppSharePreferenceUtil.getInstance().getString(SafeCloudConstants.LOGINNAMELIST, null);
            Gson gson = new Gson();
            Type type = new TypeToken<List<LoginNameVo>>() {
            }.getType();
            List<LoginNameVo> loginNameVoList = new ArrayList<LoginNameVo>();
            loginNameVoList = gson.fromJson(json, type);
            return loginNameVoList;
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 保存用户列表
     * @param loginNameVo
     * @return
     */
    public static void setLoginNameList(List<LoginNameVo> loginNameVo) {
        try {
            Gson gson = new Gson();
            String json = gson.toJson(loginNameVo);
            AppSharePreferenceUtil.getInstance().putString(SafeCloudConstants.LOGINNAMELIST, json);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取company_id
     */
    public static String getCompanyId() {

        User user = getUser();
        if (user != null) {
            return user.getCompanyId();
        } else {
            return "";
        }
    }

    /**
     * 获取company_code
     */
    public static String getCompanyCode() {

        User user = getUser();
        if (user != null) {
            return user.getCompanyCode(); //bind code == company code
        } else {
            return "";
        }
    }

    /**
     * 获取user type
     */
    public static String getUserType() {

        User user = getUser();
        if (user != null) {
            return user.getUserType();
        } else {
            return "";
        }
    }

    /**
     * 获取userID
     */
    public static String getUserID() {

        User user = getUser();
        if (user != null) {
            return user.getUserId();
        } else {
            return "";
        }
    }

    /**
     * 获取userName
     */
    public static String getUserName() {

        User user = getUser();
        if (user != null) {
            return user.getUserName();
        } else {
            return "";
        }
    }

    /**
     * 获取执法证件种类
     */
    public static String getTypeId() {

        User user = getUser();
        if (user != null) {
            return user.getTypeId();
        } else {
            return "";
        }
    }

    /**
     * 获取userName
     */
    public static String getUserTypeName() {

        User user = getUser();
        if (user != null) {
            return user.getUserTypeName();
        } else {
            return "";
        }
    }

    /**
     * 获取company_id
     */
    public static String getToken() {

        User user = getUser();
        if (user != null) {
            return user.getToken();
        } else {
            return "";
        }
    }

    /**
     * 获取CompanyName
     */
    public static String getCompanyName() {

        User user = getUser();
        if (user != null) {
            return user.getCompanyName();
        } else {
            return "";
        }
    }

    /**
     * 获取GovName
     */
    public static String getGovName() {

        User user = getUser();
        if (user != null) {
            return user.getGovName();
        } else {
            return "";
        }
    }


    /**
     * 保存Jpush是否绑定成功的状态
     *
     * @param status
     */
    public static void saveJPushStatus(boolean status) {
        AppSharePreferenceUtil
                .getInstance()
                .putBoolean(SafeCloudConstants.JPUSH_BIND_STATUS, status);
    }

    /**
     * 获取Jpush绑定状态
     */
    public static boolean getJPushStatus() {
        return AppSharePreferenceUtil
                .getInstance()
                .getBoolean(SafeCloudConstants.JPUSH_BIND_STATUS, false);
    }

}
